package com.example.excelimporter.controller;

import com.example.excelimporter.model.ImportResult;
import com.example.excelimporter.service.ExcelImportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/excel-import")
@Tag(name = "Excel Import", description = "API for importing Excel files")
public class ExcelImportController {

    @Autowired
    private ExcelImportService excelImportService;

    @PostMapping("/{templateId}")
    @Operation(summary = "Import Excel file", description = "Upload and process an Excel file using a specific template")
    @ApiResponse(responseCode = "200", description = "Excel file processed successfully")
    @ApiResponse(responseCode = "400", description = "Invalid file format or template ID")
    @ApiResponse(responseCode = "500", description = "Internal server error")
    public ImportResult importExcel(
            @Parameter(description = "Template ID to use for import") @PathVariable String templateId,
            @Parameter(description = "Excel file to import") @RequestParam("file") MultipartFile file) {
        try {
            return excelImportService.importExcel(file, templateId);
        } catch (Exception e) {
            ImportResult result = new ImportResult();
            result.setErrorMessage(e.getMessage());
            return result;
        }
    }
}
